﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using System.Collections;
using Advisor.Entities;

using NHibernate.Criterion;

namespace Advisor.Entities
{
    public class UserDAO
    {
        private Users m_User;
        public UserDAO(Users user)
        {
            this.m_User = user;
        }

        /// <summary>
        /// Retorna todas os registros da tabela Users. 
        /// </summary>
        /// <param name="column"></param>
        /// <param name="maximumRows"></param>
        /// <param name="startRowIndex"></param>
        /// <returns></returns>
        internal IList GetUsers(FilterCollection filters)
        {
            ISession session = NHibernateSession.GetSession();
            ICriteria c = session.CreateCriteria(typeof(Users));

            foreach (ICriterion filter in filters)
            {
                c.Add(filter);
            }
            return c.List();
        }

        /// <summary>
        ///  Método para inserir um User no banco. 
        /// </summary>
        /// <param name="user"></param> 

        internal void InsertUser(Users user)
        {
            ISession session = NHibernateSession.GetSession();
            session.Save(user);
            session.Close();
        }
        /// <summary>
        /// Retorna uma instância da tabela Users passando o id;
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        internal Users GetUser(FilterCollection filters)
        {
            ISession session = NHibernateSession.GetSession();
            ICriteria c = session.CreateCriteria(typeof(Users));

            foreach (ICriterion filter in filters)
            {
                c.Add(filter);
            }
            return c.UniqueResult<Users>();
        }


        /// <summary>
        /// Remove um User do banco
        /// </summary>
        /// <param name="user"></param>
        internal void DeleteUser(Users user)
        {
            ISession sessions = NHibernateSession.GetSession();
            sessions.Delete(user);
            sessions.Close();
        }

        /// <summary>
        /// Atualiza um User do banco. 
        /// </summary>
        /// <param name="user"></param>
        internal void UpdateUser(Users user)
        {
            ISession session = NHibernateSession.GetSession();
            session.BeginTransaction();
            bool beginTransaction = false;
            try
            {
                beginTransaction = true;
                session.Update(user);
                session.Transaction.Commit();
                //Comando que ativa as alterações no banco de dados.
            }
            catch (Exception)
            {
                if (beginTransaction)
                {
                    session.Transaction.Rollback();
                }

                throw;
            }
            finally
            {

                session.Close();
            }
        }

    }
}
